//
//  CycUnit.m
//  Cycling
//
//  Created by cjg.mini on 2023/4/11.
//

#import "CycUnit.h"
#import "CycLanguage.h"

@implementation CycUnit
+ (void)setUseMetricUnit:(BOOL)userMetricUnit {
    [[NSUserDefaults standardUserDefaults] setBool:userMetricUnit forKey:@"CycUseMetricUnit"];
}

+ (BOOL)useMetricUnit {
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"CycUseMetricUnit"]) {
        return [[NSUserDefaults standardUserDefaults] boolForKey:@"CycUseMetricUnit"];
    }
    //如果第一次打开应用,除了系统语言是中文,都默认显示英制单位
    NSString *language = [[CycLanguage sharedInstance] currentLanguage];
    if ([language hasPrefix:CycChinesePrefix]) {
        [self setUseMetricUnit:true];
        return true;
    } else {
        [self setUseMetricUnit:false];
        return false;
    }
}

+ (double)temperatureForValue:(double)value {
    if (self.useMetricUnit) {
        return value;
    }
    return value * 1.8 + 32;
}

+ (NSString *)temperatureUnit {
    if (self.useMetricUnit) {
        return @"℃";
    }
    return @"℉";
}

+ (double)speedForValue:(double)value {
    if (self.useMetricUnit) {
        return value;
    }
    
    double speed = value * 0.6213712;
    return speed;
}

+ (double)speedForValue1:(double)value {
    if (self.useMetricUnit) {
        return value;
    }
    
    // 更改：去尾转换
    int speed = (int)value * 0.6213712;
    return (double)speed;
}

+ (NSString *)speedUnit {
    if (self.useMetricUnit) {
        return @"km/h";
    }
    return @"mph";
}

+ (double)energyConsumptionForValue:(double)value {
    if (self.useMetricUnit) {
        return value;
    }
    return value / 0.6213712;
}

+ (NSString *)energyConsumptionUnit {
    if (self.useMetricUnit) {
        return @"W·h/km";
    }
    return @"W·h/mi";
}

+ (double)mileageForValue:(double)value {
    if (self.useMetricUnit) {
        return value;
    }
    return value * 0.6213712;
}

+ (NSString *)mileageUnit {
    if (self.useMetricUnit) {
        return @"km";
    }
    return @"mi";
}

+ (NSString *)timeStringForInterval:(double)i {
    int interval = i;
    if (interval < 3600) {
        return [NSString stringWithFormat:@"%02d'%02d\"",
                (interval / 60) % 60,
                (interval % 60)];
    } else {
        return [NSString stringWithFormat:@"%02d:%02d'",
                interval / 3600,
                (interval / 60) % 60];
    }
}

@end
